//
//  ViewController.m
//  MapBoxDemo
//
//  Created by 罗大帅 on 2017/8/8.
//  Copyright © 2017年 wenliaokeji. All rights reserved.
//

#import "ViewController.h"
#import <Mapbox/Mapbox.h>
@interface ViewController () <MGLMapViewDelegate>
@property (weak,nonatomic) MGLMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    // Do any additional setup after loading the view, typically from a nib.
    MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
    
    mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    // Set the map’s center coordinate and zoom level.
    [mapView setCenterCoordinate:CLLocationCoordinate2DMake(59.31, 18.06)
                       zoomLevel:9
                        animated:NO];
    mapView.delegate = self;
    [self.view addSubview:mapView];
    self.mapView = mapView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// Wait until the style is loaded before modifying the map style.
- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
    NSLog(@"%s",__func__);
    [self addLayer];
}
- (void)mapView:(MGLMapView *)mapView didChangeUserTrackingMode:(MGLUserTrackingMode)mode animated:(BOOL)animated{
    NSLog(@"%s",__func__);
}
- (void)mapView:(MGLMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
    NSLog(@"%s,%f",__func__,mapView.zoomLevel);
}
- (void)mapViewRegionIsChanging:(MGLMapView *)mapView{
    NSLog(@"%s,%f",__func__,mapView.zoomLevel);
}
- (void)mapView:(MGLMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    NSLog(@"%s,%f",__func__,mapView.zoomLevel);
}
- (void)addLayer {
//    MGLSource *source = [[MGLVectorSource alloc] initWithIdentifier:@"population" configurationURL:[NSURL URLWithString:@"mapbox://examples.8fgz4egr"]];
    MGLSource *source = [[MGLVectorSource alloc] initWithIdentifier:@"population" configurationURL:[NSURL URLWithString:@"https://www.mapbox.com/ios-sdk/images/runtime-circle-styles.png"]];
    
    NSDictionary *ethnicities = @{
                                  @"White": [UIColor colorWithRed: 251/255.0 green: 176/255.0 blue: 59/255.0 alpha: 1.0],
                                  @"Black": [UIColor colorWithRed: 34/255.0 green: 59/255.0 blue: 83/255.0 alpha: 1.0],
                                  @"Hispanic": [UIColor colorWithRed: 229/255.0 green: 94/255.0 blue: 94/255.0 alpha: 1.0],
                                  @"Asian": [UIColor colorWithRed: 59/255.0 green: 178/255.0 blue: 208/255.0 alpha: 1.0],
                                  @"Other": [UIColor colorWithRed: 204/255.0 green: 204/255.0 blue: 204/255.0 alpha: 1.0],
                                  };
    
    
    [self.mapView.style addSource:source];
    
    // Create a new layer for each ethnicity/circle color.
    for (NSString *key in [ethnicities allKeys]) {
        // Each layer should have a unique identifier.
        MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithIdentifier:[NSString stringWithFormat:@"population-%@", key] source:source];
        
        // Specifying the `sourceLayerIdentifier` is required for a vector tile source. This is the json attribute that wraps the data in the source.
        layer.sourceLayerIdentifier = @"sf2010";
        
        // Use a style function to smoothly adjust the circle radius from 2pt to 180pt between zoom levels 12 and 22. The `interpolationBase` parameter allows the values to interpolate along an exponential curve.
        layer.circleRadius = [MGLStyleValue valueWithInterpolationMode:MGLInterpolationModeExponential
                                                           cameraStops:@{
                                                                         @12: [MGLStyleValue valueWithRawValue:@2],
                                                                         @22: [MGLStyleValue valueWithRawValue:@180]
                                                                         }
                                                               options: @{MGLStyleFunctionOptionDefaultValue:@1.75}];
        layer.circleOpacity = [MGLStyleValue valueWithRawValue:@0.7];
        
        // Set the circle color to match the ethnicity.
        layer.circleColor = [MGLStyleValue valueWithRawValue:ethnicities[key]];
        
        // Use an NSPredicate to filter to just one ethnicity for this layer.
        layer.predicate = [NSPredicate predicateWithFormat:@"ethnicity == %@", key];
        
        [self.mapView.style addLayer:layer];
    }
}


@end
